<?php

namespace app\store\controller\game;

use app\store\controller\Controller;
use app\store\service\game\Room as RoomService;
use think\App;
use think\db\exception\DbException;
use think\response\Json;


class Room extends Controller
{

    private RoomService $roomService;
    public function __construct(App $app,RoomService $roomService)
    {
        parent::__construct($app);
        $this->roomService = $roomService;
    }

    /**房间列表
     * @return Json
     * @throws DbException
     */
    public function index(): Json
    {
        $params = $this->request->get();
        $this->validate(
            $params,
            [
                'page' => 'integer',
                'limit' => 'integer',
            ]
        );

        $list = $this->roomService->getIndex($params);
        return $this->renderSuccess(['list' => $list]);
    }

    /**
     * 创建房间
     * @return Json
     */
    public function save(): Json
    {
        $params = $this->request->post();
        $this->validate(
            $params,
            [
                'room_id'  => 'require',
                'room_name' => 'require',
                'device_id' => 'require',
                'game_name' => 'require',
                'web_url' => 'require',
                'web_port' => 'require',
                'num' => 'require',
                'power' => 'require',
                'server_ip' => 'require',
                'server_port' => 'require',
                'table_id' => 'require',
                'min_carrying|最低携带' => 'require',
                'first_exchange|首座兑换' => 'require',
                'id' => 'integer'
            ]
        );
        $res = $this->roomService->save($params);
        if(!$res){
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess([],'保存成功');
    }


}
